<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>编辑员工 - TMS</title>
    <link rel="stylesheet" href="/view/frame/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/view/frame/static/css/global.css?v=1.0">
    <link rel="stylesheet" href="/view/tms/static/css/add.css?v=1.0">
</head>

<body>
    <form class="layui-form" autocomplete="off">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>基本信息</legend>
        </fieldset>

        <div class="layui-fluid">
            <div class="layui-row">
                <div class="layui-col-sm4">
                    <div class="layui-form-item layui-form-item--require">
                        <label class="layui-form-label">姓名/工号</label>
                        <div class="layui-input-block">
                            <div class="layui-col-sm8">
                                <div class="margin-right10">
                                    <input type="text" name="name" lay-verify="required" placeholder="员工姓名" class="layui-input" maxlength="20" hc-verify="userName">
                                </div>
                            </div>
                            <div class="layui-col-sm4">
                                <input type="text" name="positionNo" placeholder="工号" class="layui-input" maxlength="20">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item layui-form-item--require">
                        <label class="layui-form-label">身份证号</label>
                        <div class="layui-input-block">
                            <div class="layui-col-sm10">
                                <div class="margin-right10">
                                    <input type="text" name="idNo" lay-verify="required" placeholder="身份证号" class="layui-input" maxlength="18" hc-verify="idNumber">
                                </div>
                            </div>
                            <div class="layui-col-sm2">
                                <input type="text" name="sex" class="layui-input" maxlength="1" disabled>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">出生日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="birthday" id="birthday" class="layui-input" disabled>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-row">
                <div class="layui-col-sm4">
                    <div class="layui-form-item layui-form-item--require">
                        <label class="layui-form-label">手机号码</label>
                        <div class="layui-input-block">
                            <input type="text" name="mobile" lay-verify="required" class="layui-input" maxlength="11" hc-verify="mobile">
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item layui-form-item--require">
                        <label class="layui-form-label">所属公司</label>
                        <div class="layui-input-block">
                            <select name="companyId" lay-verify="required|companyId" lay-filter="companyId">
                              <option value="">请选择所属公司</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item layui-form-item--require">
                        <label class="layui-form-label">所属部门</label>
                        <div class="layui-input-block">
                            <select name="departmentId" lay-verify="required|departmentId" lay-filter="departmentId">
                              <option value="">请选择部门</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-row">
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">现任职位</label>
                        <div class="layui-input-block">
                            <input type="text" name="position" lay-verify="position" class="layui-input" maxlength="100" hc-verify="positionSize">
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入职时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="positionDate" id="positionDate" placeholder="YYYY-MM-DD" maxlength="10" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">固话</label>
                        <div class="layui-input-block">
                            <input type="text" name="tel" placeholder="+86-区号-电话号码-分机号" class="layui-input" maxlength="20">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-row">
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">电子邮箱</label>
                        <div class="layui-input-block">
                            <input type="text" name="email" placeholder="" class="layui-input" maxlength="100" hc-verify="email">
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">微信号</label>
                        <div class="layui-input-block">
                            <input type="text" name="wechat" placeholder="" class="layui-input" maxlength="100" hc-verify="wechat">
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">QQ</label>
                        <div class="layui-input-block">
                            <input type="text" name="qq" placeholder="" class="layui-input" maxlength="20" hc-verify="qq">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-row">
                <div class="layui-col-sm8">
                    <div class="layui-form-item" id="linkDistrict">
                        <label class="layui-form-label">联系地址</label>
                        <div class="layui-input-block">
                            <div class="layui-col-sm3">
                                <div class="margin-right10">
                                    <select name="linkProvince" lay-filter="linkProvince">
                                      <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-sm3">
                                <div class="margin-right10">
                                    <select name="linkCity" lay-filter="linkCity">
                                      <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-sm3">
                                <div class="margin-right10">
                                    <select name="linkCounty" lay-filter="linkCounty">
                                      <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-sm3">
                                <div>
                                    <select name="linkStreet" lay-filter="linkStreet">
                                      <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="margin-left10">
                        <input type="hidden" name="linkAreaId">
                        <input type="text" name="linkAddress" placeholder="详细地址" class="layui-input" maxlength="255">
                    </div>
                </div>
            </div>

            <div class="layui-row">
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">籍贯/政治面貌</label>
                        <div class="layui-input-block">
                            <div class="layui-col-sm6">
                                <div class="margin-right10">
                                    <input type="text" name="nativePlace" placeholder="籍贯" class="layui-input" maxlength="200">
                                </div>
                            </div>
                            <div class="layui-col-sm6">
                                <input type="text" name="politicsStatus" lay-verify="politicsStatus" placeholder="政治面貌" class="layui-input" maxlength="20">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">毕业院校</label>
                        <div class="layui-input-block">
                            <input type="text" name="schoolTag" placeholder="" class="layui-input" maxlength="100">
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">应急联系人及关系</label>
                        <div class="layui-input-block">
                            <div class="layui-col-sm7">
                                <div class="margin-right10">
                                    <input type="text" name="juryUname" placeholder="联系人姓名" class="layui-input" maxlength="20" hc-verify="userName">
                                </div>
                            </div>
                            <div class="layui-col-sm5">
                                <input type="text" name="juryRelation" placeholder="关系" class="layui-input" maxlength="20">
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-row">
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">应急联系人手机</label>
                        <div class="layui-input-block">
                            <input type="text" name="juryMobile" placeholder="" class="layui-input" maxlength="11" hc-verify="mobile">
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm8">
                    <div class="layui-form-item">
                        <label class="layui-form-label">应急联系人地址</label>
                        <div class="layui-input-block">
                            <div class="layui-col-sm8" id="juryDistrict">
                                <div class="layui-col-sm3">
                                    <div class="margin-right10">
                                        <select name="juryProvince" lay-filter="juryProvince">
                                          <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-col-sm3">
                                    <div class="margin-right10">
                                        <select name="juryCity" lay-filter="juryCity">
                                          <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-col-sm3">
                                    <div class="margin-right10">
                                        <select name="juryCounty" lay-filter="juryCounty">
                                          <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-col-sm3">
                                    <div>
                                        <select name="juryStreet" lay-filter="juryStreet">
                                          <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-sm4">
                                <div class="margin-left10">
                                    <input type="hidden" name="juryAreaId">
                                    <input type="text" name="juryAddress" placeholder="详细地址" class="layui-input" maxlength="255">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>

        <fieldset class="layui-elem-field layui-field-title">
            <legend>平台操作信息</legend>
        </fieldset>

        <div class="layui-fluid">
            <div class="layui-row">
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">登录账号/密码</label>
                        <div class="layui-input-block">
                            <div class="layui-col-sm6">
                                <div class="margin-right10">
                                    <input type="text" name="accid" style="display:none;">
                                    <input type="text" name="accid" placeholder="登录账号" class="layui-input" maxlength="20" hc-verify="accountRegex">
                                </div>
                            </div>
                            <div class="layui-col-sm6">
                                <input type="password" name="accpwd" style="display:none;">
                                <input type="password" name="accpwd" placeholder="登录密码" class="layui-input" maxlength="20" hc-verify="pwdRegex|pwdSize">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item layui-form-item--require">
                        <label class="layui-form-label">数据等级</label>
                        <div class="layui-input-block">
                            <select name="permiCode" lay-verify="permiCode">
                              <option value="0" selected="selected">初级（仅看自己录入的数据）</option>
                              <option value="1">中级（能查看本部门的数据）</option>
                              <option value="2">高级（能看到所在公司的数据）</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">所属客户</label>
                        <div class="layui-input-block">
                            <select name="customersId" lay-filter="customersId">
                              <option value="">请选择所属客户</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <input type="hidden" name="id">
        <div id="buttonBar" class="layui-form-item button-bar" style="margin:10px 0 40px;">
            <button class="layui-btn layui-btn-normal" id="vsubmit" lay-submit lay-filter="submit">保存</button>
            <button class="layui-btn" id="setting" type="button" style="display:none;">保存且设置角色</button>
            <button class="layui-btn layui-btn-primary" id="vcancel">取消</button>
        </div>

        <div class="form-txt" style="text-align:center; border-top:#eee 1px solid; line-height:30px; display:none;">
            注：如果员工不需要使用系统，请点击保存，如果需要使用系统，请点击保存且设置角色，不然该员工无法正常使用系统
        </div>
    </form>
</body>
<script charset="utf-8" src="/view/frame/layui/layui.js"></script>
<script charset="utf-8" src="/view/frame/static/js/jquery.min.js?v=1.0"></script>
<script charset="utf-8" src="/view/frame/static/js/lang_zh_CN.js?v=1.0"></script>
<script charset="utf-8" src="/view/frame/static/js/layui.district_1.js?v=1.0"></script>
<script charset="utf-8" src="/view/tms/static/js/tms_index.js?v=1219"></script>
<script charset="utf-8" src="/view/tms/static/js/tms_company.js?v=1.0"></script>
<script charset="utf-8" src="/view/tms/static/js/tms_tab.js?v=1.0"></script>

<!-- 工具库依赖 -->
<script charset="utf-8" src="/view/frame/static/js/errorCodeMap.js"></script>
<script charset="utf-8" src="/view/frame/static/js/HC.js?v=1.4"></script>
<script charset="utf-8" src="/view/tms/static/js/validator.js"></script>
<script charset="utf-8" src="/view/tms/static/js/bizUtil.js"></script>

<script>
    layui.use(['form', 'layer', 'layedit', 'laydate'], function() {
        var form = layui.form,
            layer = layui.layer,
            layedit = layui.layedit,
            laydate = layui.laydate,
            $ = layui.jquery;

        laydate.render({
            elem: '#positionDate'
        });

        //自定义验证规则
        form.verify({
            companyId: function(v) {
                if (v.length == 0) {
                    return '请选择所属公司';
                }
            },
            departmentId: function(v) {
                if (v.length == 0) {
                    return '请选择所属部门';
                }
            },
            permiCode: function(v) {
                console.log(v);
                return false;
                if (v.length == 0) {
                    return '请选择数据等级';
                }
            }
        });

        var $type = $.trim(getUrlParam('do'));
        var $isAccid = false; //判断修改时是否添加登录账号

        //初始化客户列表
        getCustomerListToSelect();

        //初始化城市和公司部门
        if ($type == 'add') {
            $('#linkDistrict').district(form);
            $('#juryDistrict').district(form);
            $('body').companyDepartment(form, [], ['companyId', 'departmentId']);
        }

        if ($type == 'edit' || $type == 'detail') {
            var $id = getUrlId($.trim(getUrlParam('id')));
            $('input[name="id"]').val($id);
            if ($type == 'detail') {
                $('form input, form select').prop('disabled', true);
                $('#buttonBar').hide();
            }
            //编辑时去掉密码的验证
            $('input[name="accpwd"]').removeAttr('hc-verify');
            $('.form-txt').hide();

            //获取数据
            $.get('/ucenter/centre/permi/employee/' + $id + '.shtml', function(d) {
                var $code = d.code,
                    $msg = d.msg,
                    $objects = d.objects;

                if ($code === 'SUCCESS') {
                    $('input[name="name"]').val($objects.name);
                    $('input[name="idNo"]').val($objects.idNo);
                    $('input[name="positionNo"]').val($objects.positionNo);
                    var $sex = '男';
                    if ($objects.sex == 'MEN') {
                        $sex = '男';
                    } else if ($objects.sex == 'WOMEN') {
                        $sex = '女';
                    }
                    var $birthday = $objects.birthday != null ? new Date($objects.birthday).format('yyyy-MM-dd') : '';
                    $('input[name="sex"]').val($sex);
                    $('input[name="birthday"]').val($birthday);
                    $('input[name="schoolTag"]').val($objects.schoolTag);
                    $('input[name="nativePlace"]').val($objects.nativePlace);
                    $('input[name="politicsStatus"]').val($objects.politicsStatus);
                    $('input[name="mobile"]').val($objects.mobile);
                    $('input[name="email"]').val($objects.email);
                    $('input[name="wechat"]').val($objects.wechat);
                    $('input[name="qq"]').val($objects.qq);
                    $('input[name="tel"]').val($objects.tel);
                    $objects.companyId != null && $objects.departmentId != null ? $('body').companyDepartment(form, [$objects.companyId, $objects.departmentId], ['companyId', 'departmentId']) : '';
                    $('input[name="position"]').val($objects.position);
                    var $positionDate = $objects.positionDate != null ? new Date($objects.positionDate).format('yyyy-MM-dd') : '';
                    $('input[name="positionDate"]').val($positionDate);
                    $objects.permiCode != null ? $('select[name="permiCode"]').val($objects.permiCode) : '';
                    $objects.customersId != null ? $('select[name="customersId"]').val($objects.customersId) : '';
                    if ($objects.accid != null) {
                        $isAccid = true;
                        $('input[name="accid"]').attr('disabled', 'disabled').css({
                            'background': '#eee'
                        }).val($objects.accid);
                        $('input[name="accpwd"]').attr('disabled', 'disabled').css({
                            'background': '#eee'
                        }).val('******');
                    }
                    if ($objects.contacts != null) {
                        $objects.contacts.linkAreaId != null ? $('input[name="linkAreaId"]').val($objects.contacts.linkAreaId) : '';
                        $objects.contacts.linkAreaId != null ? (
                            $type == 'detail' ? $('#linkDistrict').district(form, $objects.contacts.linkAreaId, 0) : $('#linkDistrict').district(form, $objects.contacts.linkAreaId)
                        ) : $('#linkDistrict').district(form);
                        $('input[name="linkAddress"]').val($objects.contacts.linkAddress);
                        $('input[name="juryUname"]').val($objects.contacts.juryUname);
                        $('input[name="juryRelation"]').val($objects.contacts.juryRelation);
                        $('input[name="juryMobile"]').val($objects.contacts.juryMobile);
                        $objects.contacts.juryAreaId != null ? $('input[name="juryAreaId"]').val($objects.contacts.juryAreaId) : '';
                        $objects.contacts.juryAreaId != null ? (
                            $type == 'detail' ? $('#juryDistrict').district(form, $objects.contacts.juryAreaId, 0) : $('#juryDistrict').district(form, $objects.contacts.juryAreaId)
                        ) : $('#juryDistrict').district(form);
                        $('input[name="juryAddress"]').val($objects.contacts.juryAddress);
                    }

                    form.render('radio');
                    form.render('select');
                } else {
                    setLayerAlert(parent.layer, '数据异常', function() {
                        parent.layer.closeAll();
                    });
                    return false;
                }
            }, 'json');
        }

        if ($type == 'edit' || $type == 'add') {
            //身份证失焦后获取性别和出生年月，第17位是性别，奇数男，偶数女，第7到14位是出生年月
            $('input[name="idNo"]').on('blur', function() {
                var $idNo = $.trim($(this).val());
                if ($idNo.length == 18) {
                    var $sex = parseInt($idNo.substr(16, 1)) % 2 == 1 ? "男" : "女";
                    var $birthday = $idNo.substr(6, 4) + '-' + $idNo.substr(10, 2) + '-' + $idNo.substr(12, 2);
                    $('input[name="sex"]').val($sex);
                    $('input[name="birthday"]').val($birthday);
                }
            });

            //监听提交
            form.on('submit(submit)', function(data) {
                if (!bizUtil.validator.verifyContainer($('form'))) {
                    return false;
                }

                //城市ID
                var $index1 = $('#linkDistrict select').not(':disabled').length - 1,
                    $indexVal1 = $('#linkDistrict select').eq($index1).val();
                $indexVal1.length > 0 ? $('input[name="linkAreaId"]').val($indexVal1) : '';
                if ($.trim(data.field.linkAddress).length > 0) {
                    if ($('input[name="linkAreaId"]').val().length == 0) {
                        layer.msg('请选择联系地址的城市');
                        return false;
                    }
                }

                var $index2 = $('#juryDistrict select').not(':disabled').length - 1,
                    $indexVal2 = $('#juryDistrict select').eq($index2).val();
                $indexVal2.length > 0 ? $('input[name="juryAreaId"]').val($indexVal2) : '';
                if ($.trim(data.field.juryAddress).length > 0) {
                    if ($('input[name="juryAreaId"]').val().length == 0) {
                        layer.msg('请选择应急联系人地址的城市');
                        return false;
                    }
                }

                var $linkAreaId = $('input[name="linkAreaId"]').val().length > 0 ? parseInt($.trim($('input[name="linkAreaId"]').val())) : '',
                    $juryAreaId = $('input[name="juryAreaId"]').val().length > 0 ? parseInt($.trim($('input[name="juryAreaId"]').val())) : '';

                var $sex = $.trim(data.field.sex) == '男' ? 0 : 1;

                //数据源
                var $saveData = {
                    name: $.trim(data.field.name),
                    idNo: $.trim(data.field.idNo),
                    positionNo: $.trim(data.field.positionNo),
                    sex: $sex,
                    birthday: $.trim(data.field.birthday),
                    schoolTag: $.trim(data.field.schoolTag),
                    nativePlace: $.trim(data.field.nativePlace),
                    politicsStatus: $.trim(data.field.politicsStatus),
                    mobile: $.trim(data.field.mobile),
                    email: $.trim(data.field.email),
                    wechat: $.trim(data.field.wechat),
                    qq: $.trim(data.field.qq),
                    tel: $.trim(data.field.tel),
                    companyId: parseInt($.trim(data.field.companyId)),
                    departmentId: parseInt($.trim(data.field.departmentId)),
                    position: $.trim(data.field.position),
                    positionDate: $.trim(data.field.positionDate),
                    permiCode: parseInt($.trim(data.field.permiCode)),
                    customersId: $.trim(data.field.customersId).length > 0 ? parseInt($.trim(data.field.customersId)) : 0,
                    contacts: {
                        linkAreaId: $linkAreaId,
                        linkAddress: $.trim(data.field.linkAddress),
                        juryUname: $.trim(data.field.juryUname),
                        juryRelation: $.trim(data.field.juryRelation),
                        juryMobile: $.trim(data.field.juryMobile),
                        juryAreaId: $juryAreaId,
                        juryAddress: $.trim(data.field.juryAddress)
                    }
                };
                if (!$isAccid) {
                    $saveData.accid = $.trim(data.field.accid);
                    $saveData.accpwd = $.trim(data.field.accpwd);
                }

                var $url, $method;
                if ($type == 'edit') {
                    $url = '/ucenter/centre/permi/employee/' + $.trim(data.field.id) + '.shtml';
                    $method = 'PUT';
                } else if ($type == 'add') {
                    $url = '/ucenter/centre/permi/employee/add.shtml';
                    $method = 'POST';
                }

                //保存数据。注意：HC.ajax 的请求方法名均为小写
                HC.ajax[$method.toLowerCase()]({
                    url: $url,
                    data: $saveData,
                    success: function() {
                        setLayerAlert(parent.layer, '保存成功', {
                            yes: function(index) {
                                parent.layer.close(index);
                                bizUtil.frame.refreshListFrame();
                                bizUtil.frame.closeCurrentIframeTab(window.parent);
                            }
                        });
                    }
                });

                return false;
            });
        }

        //客户下拉框数据
        function getCustomerListToSelect() {
            var arrs = getCustomerList();
            if (arrs.length > 0) {
                for (var i = 0; i < arrs.length; i++) {
                    $('select[name="customersId"]').append('<option value="' + arrs[i].id + '">' + arrs[i].nameShort + '</option>');
                }
                form.render('select', 'customersId');
            }
        }

        //取消按钮事件
        $('#vcancel').on('click', function() {
            bizUtil.frame.closeCurrentIframeTab(window.parent);
        });

    });
</script>

</html>